import 'package:get/get.dart';
import 'package:pornhub_app/components/pay/enum_pay.dart';
import 'package:pornhub_app/components/pay/model/gold_model.dart';
import 'package:pornhub_app/components/pay/model/recharge_type_model.dart';
import 'package:pornhub_app/components/pay/model/vip_gold_type_model.dart';
import 'package:pornhub_app/components/pay/pay_view.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/services/storage_service.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:pornhub_app/views/mine/vip/buy_hint_dialog.dart';

class RechargePageController extends GetxController {
  final userService = Get.find<UserService>();
  final storageService = Get.find<StorageService>();
  var vipGold = GoldModel().obs;
  var goldVipList = <GoldModel>[].obs;
  var payUrl = ''.obs;

  @override
  void onInit() {
    super.onInit();
    _getVipCardList();
  }

  @override
  void onReady() {
    super.onReady();
    showHintDialog();
  }

  showHintDialog() {
    Get.bottomSheet(
      isScrollControlled: true,
      BuyHintDialog(onTap: () {}),
    );
  }

  Future _getVipCardList() async {
    try {
      final result = await Api.getVipGoldCards();
      if (result is VipGoldTypeModel) {
        goldVipList.value = result.goldVipList ?? [];
        if (goldVipList.isNotEmpty) {
          for (var i = 0; i < goldVipList.length; i++) {
            goldVipList[i].types?.add(RechargeTypeModel.balance());
          }
          goldVipList[0].isSelected = true;
          vipGold.value = goldVipList[0];
        }
        update();
      }
    } catch (_) {}
  }

  void showBuyHintDialog() {
    Get.bottomSheet(
      isScrollControlled: true,
      BuyHintDialog(onTap: () {
        onSubmitGold();
      }),
    );
  }

  void onSubmitGold() {
    Get.bottomSheet(
      PayView(
        amount: vipGold.value.price ?? 0,
        payId: vipGold.value.goldId ?? 0,
        purType: PurTypeEnum.gold,
        payType: vipGold.value.types ?? [],
      ),
    );
  }
}
